﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using NomClasses;

namespace NomCafe
{
    public partial class frmAreaManager : DevComponents.DotNetBar.Office2007RibbonForm
    {
        string nameArea;

        public frmAreaManager()
        {
            InitializeComponent();
        }

        private void frmAreaManager_Load(object sender, EventArgs e)
        {
            getDataArea();
        }

        private void getDataArea()
        {
            listViewArea.Items.Clear();
            // Lấy danh sách khu vực cho listViewMode            
            NomClasses.NomDataContext nc = new NomClasses.NomDataContext();
            System.Data.Linq.Table<NomClasses.nom_area> areaList = nc.nom_areas;
            foreach (NomClasses.nom_area area in areaList)
            {
                listViewArea.Items.Add(area.name).SubItems.Add(area.description);
            }
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            try
            {
                NomDataContext nc = new NomDataContext();
                nom_area area = nc.nom_areas.Single(i => i.name == nameArea);
                nc.nom_areas.DeleteOnSubmit(area);
                nc.SubmitChanges();
                
                btnRenew_Click(sender, e);
            }
            catch (Exception)
            {
                MessageBox.Show(languageManager.DELETE_AREA_ERROR, "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Hide();
        }

        private void btnAccept_Click(object sender, EventArgs e)
        {
            bool flag = false;
            try
            {
                if (txtNameArea.Text != "")
                {
                    NomDataContext nc = new NomDataContext();
                    var areaList = from area in nc.nom_areas
                                   select area;
                    foreach (var i in areaList)
                    {
                        if (txtNameArea.Text.Trim().ToLower().Equals(i.name.Trim().ToLower()))
                        {
                            flag = true;
                        }
                    }
                    if (flag == true)
                    {
                        MessageBox.Show(languageManager.ALREADY_AREA_ERROR, "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                    else
                    {
                        nom_area area = new nom_area();
                        area.name = txtNameArea.Text;
                        area.description = txtNoteArea.Text;
                        nc.nom_areas.InsertOnSubmit(area);
                        nc.SubmitChanges();
                                                
                        getDataArea();

                        txtNameArea.Text = "";
                        txtNoteArea.Text = "";
                        txtNameArea.Focus();
                    }
                }
                else
                {
                    MessageBox.Show(languageManager.INSERT_AREA_ERROR, "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

        private void listViewArea_SelectedIndexChanged(object sender, EventArgs e)
        {
            btnDelete.Enabled = true;
            btnEdit.Enabled = true;
            btnAccept.Enabled = false;

            nameArea = "";
            for (int i = 0; i < listViewArea.Items.Count; i++)
            {
                if (listViewArea.Items[i].Selected)
                {
                    nameArea = listViewArea.Items[i].SubItems[0].Text;
                    txtNameArea.Text = nameArea;
                    txtNoteArea.Text = listViewArea.Items[i].SubItems[1].Text;
                }
            }
        }

        private void btnRenew_Click(object sender, EventArgs e)
        {
            getDataArea();

            txtNameArea.Text = "";
            txtNoteArea.Text = "";

            btnDelete.Enabled = false;
            btnEdit.Enabled = false;
            btnAccept.Enabled = true;
            btnRenew.Enabled = true;
        }

        private void btnEdit_Click(object sender, EventArgs e)
        {
            bool flag = false;
            try
            {
                NomDataContext nc = new NomDataContext();
                var areaList = from area in nc.nom_areas
                               where area.name != nameArea
                               select area;
                foreach (var i in areaList)
                {
                    if (txtNameArea.Text.Trim().ToLower().Equals(i.name.Trim().ToLower()))
                    {
                        flag = true;
                    }
                }
                if (flag == true)
                {
                    MessageBox.Show(languageManager.ALREADY_AREA_ERROR, "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                else
                {
                    nom_area area = nc.nom_areas.Single(i => i.name == nameArea);
                    area.name = txtNameArea.Text;
                    area.description = txtNoteArea.Text;
                    nc.SubmitChanges();

                    btnRenew_Click(sender, e);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }
    }
}
